package fr.thread.threadfactory.activity;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import fr.thread.threadfactory.R;
import fr.thread.threadfactory.activity.intent.CallBackActivity;
import fr.thread.threadfactory.activity.note.NoteActivity;

/**
 * This activity is dedicated to Intents usage
 * @author thread
 *
 */
public class IntentsActivity extends Activity {
	
	private static final int CODE_CHILD_ACTIVITY_FOR_RESULTS = 1;
	
	 private static final int NOTIFICATION_DEFAULT = 1;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        
        setContentView(R.layout.intentsactivity);
    }
    
    
    
    public void onClickActivity(View target){
    	Intent intent = new Intent(getApplicationContext(), NoteActivity.class);
    	startActivity(intent);
    }
    
    public void clickOnActivityForResults(View target){
    	Intent intent = new Intent(getApplicationContext(), CallBackActivity.class);
    	startActivityForResult(intent,CODE_CHILD_ACTIVITY_FOR_RESULTS);
    }
    
    @Override
    public void onActivityResult(int requestCode,int resultCode, Intent data){
    	// intent is a bundle for results data
    	if (requestCode == CODE_CHILD_ACTIVITY_FOR_RESULTS){ // this is called when user made back or give a response 
    		String text = "";
    		if (RESULT_CANCELED == resultCode){ // back or cancel
    			text += "Canceled";
    		}else if (RESULT_OK == resultCode){ // yes
    			text+= "Ok";
    		}
    		createNotification("Vous avez choisi: "+text);
    		//Toast.makeText(getApplicationContext(), "Vous avez choisi: "+text, Toast.LENGTH_LONG).show();
    	}
    }
    
    private void createNotification(String text){
		//final Resources res = getResources();
		final NotificationManager notifManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
		Notification.Builder builder = new Notification.Builder(this);
		builder.setAutoCancel(true);
		builder.setContentTitle("Ma Notif");
		builder.setContentText(text);
		builder.setSmallIcon(R.drawable.ic_launcher);
		
		
		notifManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
	}

}
